﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Admins
{
    public partial class Orders : System.Web.UI.Page
    {
        static string Id = "";
        static string Lang = "";
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa ?');");
                lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa ?');");
                BinGrid();
            }

        }
        private void BinGrid()
        {
            grdOrders.DataSource = OrdersService.Order_Member();
            grdOrders.DataBind();
            if (grdOrders.PageCount <= 1)
            {
                grdOrders.PagerStyle.Visible = false; 
            }
        }
        protected string Format_Frice(string money)
        {
            if (money.Length > 3)
            {
                int tien = money.Length;
                while (tien > 3)
                {
                    money = money.Insert(tien - 3, ".");
                    tien = tien - 3;
                }
                return money + "VND";
            }
            else
            {
                return money + "VND";
            }
        }

        protected void grdOrders_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdOrders.CurrentPageIndex = e.NewPageIndex;
            BinGrid();
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            pnViews.Visible = false;
            Insert = true;
        }
        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            CheckBox chk;
            foreach (DataGridItem item in this.grdOrders.Items)
            {
                chk = (CheckBox)item.FindControl("ChkSelect");
                if (chk.Checked == true)
                {
                    string str = item.Cells[1].Text;
                    OrdersService.Orders_Delete(str);
                }
                grdOrders.CurrentPageIndex = 0;
                BinGrid();
            }
        }
        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BinGrid();
        }

        protected void grdOrders_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            this.txtIdCus.Text = e.CommandArgument.ToString();
            switch (e.CommandName)
            { 
                case "Edit":
                    Id = strCA;
                    Insert = false;
                    List<Data.Orders> listOr = OrdersService.Orders_GetById(strCA);
                    if (listOr.Count > 0)
                    {
                        List<Data.Member> listCus = MemberService.Member_GetById(listOr[0].CustormerId);
                        grdCustormer.DataSource = listCus;
                        grdCustormer.DataBind();
                        listCus.Clear();
                        listCus = null;
                    }
                    List<Data.OrderDetail> listOrderDetail = OrderDetailService.OrderDetail_GetByTop("", "OrderId = '" + strCA + "'", "");
                    grdShopcard.DataSource = listOrderDetail;
                    grdShopcard.DataBind();
                    Double money = 0;
                    for (int i = 0; i < listOrderDetail.Count; i++)
                    {
                        money += Double.Parse(listOrderDetail[i].TotalMoney.ToString());
                    }
                    this.lblsumpay.Text = Format_Frice(money.ToString());
                    listOrderDetail.Clear();
                    listOrderDetail = null;
                    listOr.Clear();
                    listOr = null;
                    pnViews.Visible = false;
                    pnUpdate.Visible = true;
                    break;
                case "Delete":
                    try
                    {
                        OrdersService.Orders_Delete(strCA);
                        BinGrid();
                    }
                    catch
                    {
                        WebMsgBox.Show("Đơn hàng đã có người đặt, bạn không thể xóa !");
                        return;
                    }
                    break;
            }
        }

        protected void grdOrders_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdOrders.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
                    }
                }
            }
        }
        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                List<Data.Orders> listOr = OrdersService.Orders_GetById(txtIdCus.Text.Trim());
                if (listOr.Count > 0)
                {
                    Data.Orders obj = new Data.Orders();
                    obj.Id = listOr[0].Id;
                    obj.CustormerId = listOr[0].CustormerId;
                    obj.TotalMoney = listOr[0].TotalMoney;
                    obj.CreatDate = DateTimeClass.ConvertDateTime(DateTime.Now, "MM/dd/yyyy HH:mm:ss");
                    obj.DeliveryDate = DateTimeClass.ConvertDateTime(listOr[0].DeliveryDate, "MM/dd/yyyy HH:mm:ss");
                    obj.Status = "1";
                    obj.Tag = "1";
                    obj.Lang = "";
                    if (Insert == true)
                    {
                        OrdersService.Orders_Insert(obj);
                    }
                    else
                    {
                        OrdersService.Orders_Update(obj);
                    }
                    BinGrid();
                    pnViews.Visible = true;
                    pnUpdate.Visible = false;
                    Insert = false;
                    this.txtIdCus.Text = "";
                }
            }
        }
        protected string Status(string stt)
        {
            if (stt.ToString().Equals("1"))
            {
                return "Đã giao hàng";
            }
            else
            {
                return "Chưa giao hàng";
            }
        }
        protected string Tensp(string ProId)
        {
            List<Data.Products> listpro = ProductsService.Products_GetById(ProId);
            return listpro[0].Name.ToString();
        }
        protected void Back_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnViews.Visible = false;
            Insert = false;
        }
    }
}